<?php

use app\model\SysconfigModel;

Class Baidu_AI{
    //api_key
    protected $api_key;
    protected $secret_key;

    //api_key_face
    protected $api_key_face;
    protected $secret_key_face;

    public function __construct(){    
        $this->api_key = SysconfigModel::where('name','Baidu_Apikey')->value('val');
        $this->secret_key = SysconfigModel::where('name','Baidu_Secretkey')->value('val');

        $this->api_key_face = SysconfigModel::where('name','Baidu_Face_Apikey')->value('val');
        $this->secret_key_face = SysconfigModel::where('name','Baidu_Face_Secretkey')->value('val');
    }    

    //身份证验证
    public function identitycard_verify( $imgurl ){
        $access_token = $this->access_token($this->api_key,$this->secret_key);
        $token = $access_token['access_token'];
        $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' . $token;
        $img = file_get_contents($imgurl);
        $img = base64_encode($img);
        $bodys = array(
            'id_card_side' => "front",
            'image' => $img
        );
        $res = $this->request_post($url, $bodys);
        // var_dump($res);

        return json_decode($res, true);
    }

    //营业执照验证
    public function business_license_verify( $imgurl ){
        $access_token = $this->access_token($this->api_key,$this->secret_key);
        $token = $access_token['access_token'];
        $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?access_token=' . $token;
        $img = file_get_contents($imgurl);
        $img = base64_encode($img);
        $bodys = array(
            'image' => $img
        );
        $res = $this->request_post($url, $bodys);
        return json_decode($res, true);
        // var_dump($res);

    }

    public function person_verify($imgurl,$identitycard,$name){
        $access_token = $this->access_token($this->api_key_face,$this->secret_key_face);
        $token = $access_token['access_token'];
        $url = 'https://aip.baidubce.com/rest/2.0/face/v3/person/verify?access_token=' . $token;
        
        $img = file_get_contents($imgurl);
        $img = base64_encode($img);
        
        // $bodys = "{\"image\":\"{$img}\",\"image_type\":\"BASE64\",\"id_card_number\":\"{$identitycard}\",\"name\":\"{$name}\",\"quality_control\":\"LOW\",\"liveness_control\":\"HIGH\"}";
        
        $bodys = array(
            'image' => $img,
            'image_type' => 'BASE64',
            'id_card_number' => $identitycard,
            'name' => $name,
            'quality_control' => 'NORMAL',
            'liveness_control' => 'HIGH',            
        );
        
        $res = $this->request_post($url, $bodys);
        // var_dump($res);        


        return json_decode($res, true);
        
    }



    private function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        $data = curl_exec($curl);//运行curl
        curl_close($curl);

        return $data;
    }


    //
    private function access_token($apikey,$secretkey){
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type'] = 'client_credentials';
        $post_data['client_id'] = $apikey;
        $post_data['client_secret'] = $secretkey;
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);

        $res = $this->request_post($url, $post_data);

        // var_dump($res);
        return json_decode($res, true);
    }

}



